package com.spring.sso.util;

import org.springframework.beans.BeansException;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static AnnotationConfigServletWebServerApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if(null != context) {
            applicationContext = (AnnotationConfigServletWebServerApplicationContext) context;
        }
    }

    public static Object getBeanByName(String className) {
        return applicationContext.getBean(className);
    }

    public static Object getBeanByType(Class clazz) {
        return applicationContext.getBean(clazz);
    }
}
